Load all required libraries.

library(tidyverse)
## -- Attaching packages --------------------------------------- tidyverse 1.3.1 --
## v ggplot2 3.3.5     v purrr   0.3.4
## v tibble  3.1.6     v dplyr   1.0.8
## v tidyr   1.2.0     v stringr 1.4.0
## v readr   2.1.2     v forcats 0.5.1
## -- Conflicts ------------------------------------------ tidyverse_conflicts() --
## x dplyr::filter() masks stats::filter()
## x dplyr::lag()    masks stats::lag()
library(plotly)
## 
## Attaching package: 'plotly'
## The following object is masked from 'package:ggplot2':
## 
##     last_plot
## The following object is masked from 'package:stats':
## 
##     filter
## The following object is masked from 'package:graphics':
## 
##     layout
library(broom)

Read in raw data from RDS.

raw_data <- readRDS("./year2.RDS")

Make a few small modifications to names and data for visualizations.

final_data <- raw_data %>% mutate(log_copy_per_L = log10(mean_copy_num_L)) %>%
  rename(Facility = wrf) %>%
  mutate(Facility = recode(Facility, 
                           "NO" = "WRF A",
                           "MI" = "WRF B",
                           "CC" = "WRF C"))

Seperate the data by gene target to ease layering in the final plot

#make three data layers
only_positives <<- subset(final_data, (!is.na(final_data$Facility)))
only_n1 <- subset(only_positives, target == "N1")
only_n2 <- subset(only_positives, target == "N2")
only_background <<-final_data %>% 
  select(c(date, cases_cum_clarke, new_cases_clarke, X7_day_ave_clarke)) %>%
  group_by(date) %>% summarise_if(is.numeric, mean)

#specify fun colors
background_color <- "#7570B3"
seven_day_ave_color <- "#E6AB02"
marker_colors <- c("N1" = '#1B9E77',"N2" ='#D95F02')
#remove facilty C for now
#only_n1 <- only_n1[!(only_n1$Facility == "WRF C"),]
#only_n2 <- only_n2[!(only_n2$Facility == "WRF C"),]

only_n1 <- only_n1[!(only_n1$Facility == "WRF A" & only_n1$date == "2020-11-02"), ]
only_n2 <- only_n2[!(only_n2$Facility == "WRF A" & only_n2$date == "2020-11-02"), ]

Build the main plot

      #first layer is the background epidemic curve
        p1 <- only_background %>%
              plotly::plot_ly() %>%
              plotly::add_trace(x = ~date, y = ~new_cases_clarke, 
                                type = "bar", 
                                hoverinfo = "text",
                                text = ~paste('</br> Date: ', date,
                                                     '</br> Daily Cases: ', new_cases_clarke),
                                alpha = 0.5,
                                name = "Daily Reported Cases",
                                color = background_color,
                                colors = background_color,
                                showlegend = FALSE) %>%
            layout(yaxis = list(title = "Clarke County Daily Cases", showline=TRUE)) %>%
            layout(legend = list(orientation = "h", x = 0.2, y = -0.3))
        
        #renders the main plot layer two as seven day moving average
        p1 <- p1 %>% plotly::add_trace(x = ~date, y = ~X7_day_ave_clarke, 
                             type = "scatter",
                             mode = "lines",
                             hoverinfo = "text",
                            text = ~paste('</br> Date: ', date,
                                                     '</br> Seven-Day Moving Average: ', X7_day_ave_clarke),
                             name = "Seven Day Moving Average Athens",
                             line = list(color = seven_day_ave_color),
                             showlegend = FALSE)
      

        
        #renders the main plot layer three as positive target hits
        
        p2 <- plotly::plot_ly() %>%
          plotly::add_trace(x = ~date, y = ~mean_copy_num_L,
                                       type = "scatter",
                                       mode = "markers",
                                       hoverinfo = "text",
                                       text = ~paste('</br> Date: ', date,
                                                     '</br> Facility: ', Facility,
                                                     '</br> Target: ', target,
                                                     '</br> Copies/L: ', round(mean_copy_num_L, digits = 2)),
                                       data = only_n1,
                                       symbol = ~Facility,
                                       marker = list(color = '#1B9E77', size = 8, opacity = 0.65),
                                       showlegend = FALSE) %>%
          plotly::add_trace(x = ~date, y = ~mean_copy_num_L,
                                       type = "scatter",
                                       mode = "markers",
                                       hoverinfo = "text",
                                       text = ~paste('</br> Date: ', date,
                                                     '</br> Facility: ', Facility,
                                                     '</br> Target: ', target,
                                                     '</br> Copies/L: ', round(mean_copy_num_L, digits = 2)),
                                       data = only_n2,
                                       symbol = ~Facility,
                                       marker = list(color = '#D95F02', size = 8, opacity = 0.65),
                                       showlegend = FALSE) %>%
            layout(yaxis = list(title = "SARS CoV-2 Copies/L", 
                                 showline = TRUE,
                                 type = "log",
                                 dtick = 1,
                                 automargin = TRUE)) %>%
            layout(legend = list(orientation = "h", x = 0.2, y = -0.3))
        
        #adds the limit of detection dashed line
        p2 <- p2 %>% plotly::add_segments(x = as.Date("2021-06-30"), 
                                          xend = ~max(date + 10), 
                                          y = 3571.429, yend = 3571.429,
                                          opacity = 0.35,
                                          line = list(color = "black", dash = "dash")) %>%
          layout(annotations = list(x = as.Date("2021-06-30"), y = 3.8, xref = "x", yref = "y", 
                                    text = "Limit of Detection", showarrow = FALSE))

        

        p1
        p2

Combine the two main plot pieces as a subplot

#seperate n1 and n2 frames by site
#n1
wrf_a_only_n1 <- subset(only_n1, Facility == "WRF A")
wrf_b_only_n1 <- subset(only_n1, Facility == "WRF B")
wrf_c_only_n1 <- subset(only_n1, Facility == "WRF C")

#n2
wrf_a_only_n2 <- subset(only_n2, Facility == "WRF A")
wrf_b_only_n2 <- subset(only_n2, Facility == "WRF B")
wrf_c_only_n2 <- subset(only_n2, Facility == "WRF C")


#rejoin the old data frames then seperate in to averages for each plant. 
wrfa_both <- full_join(wrf_a_only_n1, wrf_a_only_n2)%>%
  select(c(date, mean_total_copies)) %>%
  group_by(date) %>%
  summarize_if(is.numeric, mean) %>%
  ungroup() %>%
  mutate(log_total_copies_both = log10(mean_total_copies))
## Joining, by = c("date", "new_cases_clarke", "cases_cum_clarke",
## "X7_day_ave_clarke", "Facility", "collection_num", "target",
## "mean_copy_num_uL_rxn", "mean_copy_num_L", "sd_L", "mean_total_copies",
## "sd_total_copies", "log_copy_per_L")
wrfb_both <- full_join(wrf_b_only_n1, wrf_b_only_n2)%>%
  select(c(date, mean_total_copies)) %>%
  group_by(date) %>%
  summarize_if(is.numeric, mean) %>%
  ungroup() %>%
  mutate(log_total_copies_both = log10(mean_total_copies))
## Joining, by = c("date", "new_cases_clarke", "cases_cum_clarke",
## "X7_day_ave_clarke", "Facility", "collection_num", "target",
## "mean_copy_num_uL_rxn", "mean_copy_num_L", "sd_L", "mean_total_copies",
## "sd_total_copies", "log_copy_per_L")
wrfc_both <- full_join(wrf_c_only_n1, wrf_c_only_n2)%>%
  select(c(date, mean_total_copies)) %>%
  group_by(date) %>%
  summarize_if(is.numeric, mean) %>%
  ungroup() %>%
  mutate(log_total_copies_both = log10(mean_total_copies))
## Joining, by = c("date", "new_cases_clarke", "cases_cum_clarke",
## "X7_day_ave_clarke", "Facility", "collection_num", "target",
## "mean_copy_num_uL_rxn", "mean_copy_num_L", "sd_L", "mean_total_copies",
## "sd_total_copies", "log_copy_per_L")
#get max date
maxdate <- max(wrfa_both$date)
mindate <- min(wrfa_both$date)

Build loess smoothing figures figures

This makes the individual plots

#**************************************WRF A PLOT**********************************************
#add trendlines 
#extract data from geom_smooth
#both extract
# *********************************span 0.6***********************************
#*****************Must always update the n = TOTAL NUMBER OF DAYS*************************
extract_botha <- ggplot(wrfa_both, aes(x = date, y = log_total_copies_both)) + 
  stat_smooth(aes(outfit=fit_botha<<-..y..), method = "loess", color = '#1B9E77', 
              span = 0.25, n = 288)
## Warning: Ignoring unknown aesthetics: outfit
#look at the fits to align dates and total observations
#both
extract_botha
## `geom_smooth()` using formula 'y ~ x'

fit_botha
##   [1] 11.53705 11.57475 11.61215 11.64926 11.68607 11.72259 11.75881 11.79475
##   [9] 11.83039 11.86575 11.90081 11.93559 11.97009 12.00426 12.03811 12.07164
##  [17] 12.10488 12.13785 12.17059 12.20310 12.23545 12.26764 12.29964 12.33141
##  [25] 12.36292 12.39412 12.42499 12.45549 12.48559 12.51497 12.54349 12.57134
##  [33] 12.59871 12.62579 12.65277 12.67983 12.70795 12.73763 12.76842 12.79987
##  [41] 12.83151 12.86289 12.89355 12.92303 12.95087 12.97662 12.99982 13.02000
##  [49] 13.03983 13.06142 13.08348 13.10474 13.12394 13.13979 13.15103 13.15973
##  [57] 13.16845 13.17666 13.18379 13.18931 13.19265 13.19328 13.19063 13.18417
##  [65] 13.17222 13.15448 13.13250 13.10785 13.08205 13.05666 13.03323 13.00852
##  [73] 12.97886 12.94513 12.90820 12.86894 12.82824 12.78697 12.74601 12.70623
##  [81] 12.66850 12.63371 12.60273 12.57052 12.53308 12.49284 12.45225 12.41372
##  [89] 12.37971 12.35264 12.33077 12.31062 12.29204 12.27484 12.25887 12.24395
##  [97] 12.22992 12.21661 12.20386 12.19403 12.18866 12.18633 12.18563 12.18514
## [105] 12.18344 12.17912 12.17342 12.16851 12.16434 12.16087 12.15807 12.15591
## [113] 12.15434 12.15332 12.15282 12.15281 12.15323 12.15406 12.15511 12.15632
## [121] 12.15780 12.15967 12.16207 12.16511 12.16891 12.17395 12.18037 12.18782
## [129] 12.19596 12.20445 12.21293 12.22105 12.22848 12.23485 12.24393 12.25827
## [137] 12.27569 12.29404 12.31116 12.32489 12.33306 12.33582 12.33515 12.33162
## [145] 12.32581 12.31827 12.30958 12.30032 12.29105 12.28234 12.27477 12.26891
## [153] 12.26532 12.26458 12.26726 12.26824 12.26382 12.25676 12.24979 12.24567
## [161] 12.24713 12.25691 12.27409 12.29550 12.32051 12.34849 12.37879 12.41080
## [169] 12.44388 12.47739 12.51071 12.54320 12.57423 12.60317 12.63620 12.67873
## [177] 12.72896 12.78512 12.84540 12.90803 12.97121 13.03315 13.09207 13.14617
## [185] 13.19367 13.23277 13.26170 13.27865 13.28571 13.28671 13.28250 13.27391
## [193] 13.26180 13.24701 13.23038 13.21275 13.19498 13.17074 13.13544 13.09247
## [201] 13.04525 12.99717 12.95165 12.91208 12.87392 12.83092 12.78396 12.73391
## [209] 12.68167 12.62811 12.57410 12.52052 12.46826 12.41818 12.37118 12.32813
## [217] 12.28630 12.24293 12.19882 12.15476 12.11155 12.06998 12.03086 11.99369
## [225] 11.95740 11.92194 11.88727 11.85333 11.82007 11.78745 11.75541 11.72391
## [233] 11.69024 11.65300 11.61407 11.57537 11.53880 11.50625 11.47963 11.45705
## [241] 11.43545 11.41496 11.39571 11.37784 11.36146 11.34673 11.33376 11.32270
## [249] 11.31366 11.30679 11.30222 11.29887 11.29596 11.29409 11.29381 11.29571
## [257] 11.30037 11.30836 11.31927 11.33232 11.34747 11.36471 11.38401 11.40537
## [265] 11.42875 11.45415 11.48153 11.51091 11.54233 11.57583 11.61145 11.64923
## [273] 11.68920 11.73141 11.77586 11.82251 11.87134 11.92232 11.97541 12.03060
## [281] 12.08784 12.14716 12.20859 12.27213 12.33779 12.40557 12.47547 12.54750
#assign fits to a vector
both_trenda <- fit_botha

#extract y min and max for each
limits_botha <- ggplot_build(extract_botha)$data
## `geom_smooth()` using formula 'y ~ x'
limits_botha <- as.data.frame(limits_botha)
both_ymina <- limits_botha$ymin
both_ymaxa <- limits_botha$ymax

#reassign dataframes (just to be safe)
work_botha <- wrfa_both

#fill in missing dates to smooth fits
work_botha <- work_botha %>% complete(date = seq(min(date), max(date), by = "1 day"))
date_vec_botha <- work_botha$date

#create a new smooth dataframe to layer
smooth_frame_botha <- data.frame(date_vec_botha, both_trenda, both_ymina, both_ymaxa)
#WRF A
#plot smooth frames
p_wrf_a <- plotly::plot_ly() %>%
  plotly::add_lines(x = ~date_vec_botha, y = ~both_trenda,
                    data = smooth_frame_botha,
                    hoverinfo = "text",
                    text = ~paste('</br> Date: ', date_vec_botha,
                                  '</br> Median Log Copies: ', round(both_trenda, digits = 2)),
                    line = list(color = '#1B9E77', size = 8, opacity = 0.65),
                    showlegend = FALSE) %>%
     layout(xaxis = list(range = c(mindate - 7, maxdate + 7))) %>% #buffer here
plotly::add_ribbons(x ~date_vec_botha, ymin = ~both_ymina, ymax = ~both_ymaxa,
                    showlegend = FALSE,
                    opacity = 0.25,
                    hoverinfo = "text",
                    text = ~paste('</br> Date: ', date_vec_botha, #leaving in case we want to change
                                  '</br> Max Log Copies: ', round(both_ymaxa, digits = 2),
                                  '</br> Min Log Copies: ', round(both_ymina, digits = 2)),
                    name = "",
                    fillcolor = '#1B9E77',
                    line = list(color = '#1B9E77')) %>%
                layout(yaxis = list(title = "Total Log10 SARS CoV-2 Copies", 
                                 showline = TRUE,
                                 automargin = TRUE)) %>%
                layout(xaxis = list(title = "Date")) %>%
                layout(title = "WRF A") %>%
  plotly::add_markers(x = ~date, y = ~log_total_copies_both,
                      data = wrfa_both,
                       hoverinfo = "text",
                       showlegend = FALSE,
                       text = ~paste('</br> Date: ', date, 
                                     '</br> Actual Log Copies: ', round(log_total_copies_both, digits = 2)),
                       marker = list(color = '#1B9E77', size = 6, opacity = 0.65))

p_wrf_a
save(p_wrf_a, file = "./site_objects/wrf_a_year2.rda")
#**************************************WRF B PLOT**********************************************
#add trendlines 
#extract data from geom_smooth
#both extract
# *********************************span 0.6***********************************
#*****************Must always update the n = TOTAL NUMBER OF DAYS*************************
extract_bothb <- ggplot(wrfb_both, aes(x = date, y = log_total_copies_both)) + 
  stat_smooth(aes(outfit=fit_bothb<<-..y..), method = "loess", color = '#D95F02', 
              span = 0.25, n = 288)
## Warning: Ignoring unknown aesthetics: outfit
#look at the fits to align dates and total observations
#both
extract_bothb
## `geom_smooth()` using formula 'y ~ x'

fit_bothb
##   [1] 10.69890 10.79386 10.88682 10.97779 11.06674 11.15368 11.23860 11.32149
##   [9] 11.40234 11.48114 11.55788 11.63257 11.70518 11.77568 11.84408 11.91039
##  [17] 11.97466 12.03692 12.09720 12.15553 12.21159 12.26513 12.31630 12.36527
##  [25] 12.41218 12.45719 12.50045 12.54212 12.58236 12.62080 12.65707 12.69132
##  [33] 12.72370 12.75434 12.78340 12.81102 12.83609 12.85767 12.87613 12.89187
##  [41] 12.90526 12.91667 12.92649 12.93510 12.94288 12.95020 12.95746 12.96501
##  [49] 12.97175 12.97657 12.97984 12.98197 12.98334 12.98435 12.98539 12.98551
##  [57] 12.98382 12.98070 12.97656 12.97181 12.96685 12.96209 12.95793 12.95478
##  [65] 12.95132 12.94628 12.94012 12.93329 12.92626 12.91947 12.91340 12.90845
##  [73] 12.90450 12.90122 12.89826 12.89531 12.89202 12.88807 12.88311 12.87681
##  [81] 12.86885 12.85888 12.84658 12.83213 12.81620 12.79912 12.78123 12.76288
##  [89] 12.74441 12.72615 12.70715 12.68643 12.66434 12.64122 12.61741 12.59327
##  [97] 12.56912 12.54532 12.52220 12.49975 12.47743 12.45480 12.43144 12.40692
## [105] 12.38080 12.35265 12.32012 12.28198 12.23952 12.19402 12.14676 12.09903
## [113] 12.05209 12.00724 11.96576 11.92892 11.89802 11.87432 11.85025 11.81978
## [121] 11.78652 11.75408 11.72610 11.70618 11.69794 11.69953 11.70613 11.71687
## [129] 11.73090 11.74734 11.76535 11.78405 11.80259 11.82010 11.84497 11.88297
## [137] 11.92937 11.97941 12.02835 12.07143 12.10392 12.13141 12.16228 12.19590
## [145] 12.23167 12.26899 12.30723 12.34580 12.38407 12.42144 12.45730 12.49104
## [153] 12.52204 12.54971 12.57341 12.58893 12.59491 12.59512 12.59329 12.59318
## [161] 12.59854 12.61312 12.63404 12.65583 12.67835 12.70143 12.72494 12.74873
## [169] 12.77265 12.79656 12.82030 12.84373 12.86670 12.88907 12.91349 12.94216
## [177] 12.97422 13.00881 13.04508 13.08216 13.11920 13.15535 13.18973 13.22151
## [185] 13.24981 13.27378 13.29257 13.30531 13.31362 13.31971 13.32367 13.32560
## [193] 13.32556 13.32364 13.31994 13.31452 13.30748 13.29784 13.28496 13.26940
## [201] 13.25172 13.23250 13.21231 13.19171 13.17056 13.14831 13.12489 13.10025
## [209] 13.07432 13.04704 13.01834 12.98817 12.95646 12.92314 12.88815 12.85144
## [217] 12.81092 12.76550 12.71659 12.66561 12.61397 12.56308 12.51436 12.46151
## [225] 12.39924 12.33045 12.25808 12.18505 12.11425 12.04863 11.99109 11.94455
## [233] 11.90649 11.87185 11.83973 11.80926 11.77954 11.74968 11.71879 11.68737
## [241] 11.65663 11.62671 11.59777 11.56996 11.54344 11.51835 11.49486 11.47311
## [249] 11.45325 11.43544 11.41984 11.40820 11.40133 11.39791 11.39661 11.39611
## [257] 11.39510 11.39226 11.38680 11.37954 11.37155 11.36390 11.35767 11.35392
## [265] 11.35374 11.35819 11.36834 11.38184 11.39605 11.41163 11.42924 11.44955
## [273] 11.47322 11.50091 11.53190 11.56509 11.60062 11.63861 11.67919 11.72248
## [281] 11.76863 11.81783 11.87011 11.92535 11.98342 12.04419 12.10754 12.17333
#assign fits to a vector
both_trendb <- fit_bothb

#extract y min and max for each
limits_bothb <- ggplot_build(extract_bothb)$data
## `geom_smooth()` using formula 'y ~ x'
limits_bothb <- as.data.frame(limits_bothb)
both_yminb <- limits_bothb$ymin
both_ymaxb <- limits_bothb$ymax

#reassign dataframes (just to be safe)
work_bothb <- wrfb_both

#fill in missing dates to smooth fits
work_bothb <- work_bothb %>% complete(date = seq(min(date), max(date), by = "1 day"))
date_vec_bothb <- work_bothb$date

#create a new smooth dataframe to layer
smooth_frame_bothb <- data.frame(date_vec_bothb, both_trendb, both_yminb, both_ymaxb)
#WRF B
#plot smooth frames
p_wrf_b <- plotly::plot_ly() %>%
  plotly::add_lines(x = ~date_vec_bothb, y = ~both_trendb,
                    data = smooth_frame_bothb,
                    hoverinfo = "text",
                    text = ~paste('</br> Date: ', date_vec_bothb,
                                  '</br> Median Log Copies: ', round(both_trendb, digits = 2)),
                    line = list(color = '#D95F02', size = 8, opacity = 0.65),
                    showlegend = FALSE) %>%
     layout(xaxis = list(range = c(mindate - 7, maxdate + 7))) %>% #buffer here
plotly::add_ribbons(x ~date_vec_bothb, ymin = ~both_yminb, ymax = ~both_ymaxb,
                    showlegend = FALSE,
                    opacity = 0.25,
                    hoverinfo = "text",
                    text = ~paste('</br> Date: ', date_vec_bothb, #leaving in case we want to change
                                  '</br> Max Log Copies: ', round(both_ymaxb, digits = 2),
                                  '</br> Min Log Copies: ', round(both_yminb, digits = 2)),
                    name = "",
                    fillcolor = '#D95F02',
                    line = list(color = '#D95F02')) %>%
                layout(yaxis = list(title = "Total Log10 SARS CoV-2 Copies", 
                                 showline = TRUE,
                                 automargin = TRUE)) %>%
                layout(xaxis = list(title = "Date")) %>%
                layout(title = "WRF B") %>%
  plotly::add_markers(x = ~date, y = ~log_total_copies_both,
                      data = wrfb_both,
                       hoverinfo = "text",
                       showlegend = FALSE,
                       text = ~paste('</br> Date: ', date, 
                                     '</br> Actual Log Copies: ', round(log_total_copies_both, digits = 2)),
                       marker = list(color = '#D95F02', size = 6, opacity = 0.65))

p_wrf_b
save(p_wrf_b, file = "./site_objects/wrf_b_year2.rda")

#**************************************WRF C PLOT********************************************** #add trendlines #extract data from geom_smooth # *********************************span 0.6*********************************** #*****************Must always update the n = TOTAL NUMBER OF DAYS*************************

extract_bothc <- ggplot(wrfc_both, aes(x = date, y = log_total_copies_both)) + 
  stat_smooth(aes(outfit=fit_bothc<<-..y..), method = "loess", color = '#E7298A', 
              span = 0.25, n = 288)
## Warning: Ignoring unknown aesthetics: outfit
#look at the fits to align dates and total observations
#both
extract_bothc
## `geom_smooth()` using formula 'y ~ x'

fit_bothc
##   [1] 10.79581 10.86514 10.93303 10.99945 11.06437 11.12777 11.18961 11.24987
##   [9] 11.30853 11.36555 11.42090 11.47455 11.52649 11.57666 11.62512 11.67196
##  [17] 11.71728 11.76119 11.80378 11.84515 11.88448 11.92113 11.95548 11.98790
##  [25] 12.01876 12.04843 12.07730 12.10573 12.13410 12.16037 12.18300 12.20310
##  [33] 12.22180 12.24019 12.25941 12.28056 12.30315 12.32585 12.34854 12.37110
##  [41] 12.39342 12.41536 12.43682 12.45767 12.47779 12.49706 12.51536 12.53257
##  [49] 12.55011 12.56887 12.58792 12.60631 12.62311 12.63740 12.64822 12.65662
##  [57] 12.66417 12.67075 12.67624 12.68051 12.68346 12.68496 12.68489 12.68313
##  [65] 12.67923 12.67315 12.66536 12.65631 12.64649 12.63637 12.62641 12.61541
##  [73] 12.60206 12.58667 12.56956 12.55104 12.53142 12.51102 12.49016 12.46914
##  [81] 12.44827 12.42789 12.40829 12.38713 12.36272 12.33629 12.30912 12.28246
##  [89] 12.25756 12.23568 12.21421 12.19032 12.16494 12.13899 12.11342 12.08915
##  [97] 12.06712 12.04826 12.03350 12.02173 12.01111 12.00147 11.99266 11.98453
## [105] 11.97690 11.96963 11.96406 11.96127 11.96076 11.96202 11.96455 11.96784
## [113] 11.97137 11.97464 11.97714 11.97836 11.97780 11.97494 11.97174 11.97010
## [121] 11.96945 11.96919 11.96875 11.96754 11.96498 11.96085 11.95556 11.94943
## [129] 11.94278 11.93593 11.92921 11.92294 11.91743 11.91302 11.90887 11.90403
## [137] 11.89866 11.89291 11.88693 11.88088 11.87492 11.86777 11.85840 11.84737
## [145] 11.83519 11.82241 11.80956 11.79719 11.78581 11.77598 11.76823 11.76309
## [153] 11.76110 11.76279 11.76871 11.77645 11.78379 11.79153 11.80046 11.81138
## [161] 11.82508 11.84238 11.86374 11.88880 11.91701 11.94786 11.98080 12.01531
## [169] 12.05085 12.08690 12.12293 12.15841 12.19280 12.22558 12.26172 12.30564
## [177] 12.35593 12.41118 12.46999 12.53095 12.59267 12.65373 12.71274 12.76829
## [185] 12.81897 12.86339 12.90014 12.92781 12.94925 12.96821 12.98466 12.99855
## [193] 13.00986 13.01853 13.02453 13.02782 13.02836 13.02488 13.01676 13.00492
## [201] 12.99028 12.97377 12.95633 12.93888 12.91962 12.89647 12.86990 12.84038
## [209] 12.80837 12.77435 12.73879 12.70215 12.66492 12.62755 12.59052 12.55430
## [217] 12.51421 12.46697 12.41513 12.36123 12.30782 12.25744 12.21265 12.16709
## [225] 12.11431 12.05674 11.99678 11.93687 11.87941 11.82683 11.78153 11.74595
## [233] 11.71740 11.69145 11.66790 11.64653 11.62715 11.60954 11.59349 11.58103
## [241] 11.57380 11.57109 11.57220 11.57639 11.58296 11.59119 11.60037 11.60977
## [249] 11.61868 11.62638 11.63217 11.64414 11.66772 11.69818 11.73080 11.76085
## [257] 11.78358 11.79428 11.79496 11.79147 11.78487 11.77620 11.76651 11.75686
## [265] 11.74827 11.74182 11.73853 11.73687 11.73463 11.73191 11.72878 11.72531
## [273] 11.72159 11.71768 11.71344 11.70872 11.70355 11.69799 11.69210 11.68592
## [281] 11.67951 11.67298 11.66640 11.65971 11.65286 11.64579 11.63847 11.63083
#assign fits to a vector
both_trendc <- fit_bothc

#extract y min and max for each
limits_bothc <- ggplot_build(extract_bothc)$data
## `geom_smooth()` using formula 'y ~ x'
limits_bothc <- as.data.frame(limits_bothc)
both_yminc <- limits_bothc$ymin
both_ymaxc <- limits_bothc$ymax

#reassign dataframes (just to be safe)
work_bothc <- wrfc_both

#fill in missing dates to smooth fits
work_bothc <- work_bothc %>% complete(date = seq(min(date), max(date), by = "1 day"))
date_vec_bothc <- work_bothc$date

#create a new smooth dataframe to layer
smooth_frame_bothc <- data.frame(date_vec_bothc, both_trendc, both_yminc, both_ymaxc)
#WRF C
#plot smooth frames
p_wrf_c <- plotly::plot_ly() %>%
  plotly::add_lines(x = ~date_vec_bothc, y = ~both_trendc,
                    data = smooth_frame_bothc,
                    hoverinfo = "text",
                    text = ~paste('</br> Date: ', date_vec_bothc,
                                  '</br> Median Log Copies: ', round(both_trendc, digits = 2)),
                    line = list(color = '#E7298A', size = 8, opacity = 0.65),
                    showlegend = FALSE) %>%
     layout(xaxis = list(range = c(mindate - 7, maxdate + 7))) %>% #buffer here
plotly::add_ribbons(x ~date_vec_bothc, ymin = ~both_yminc, ymax = ~both_ymaxc,
                    showlegend = FALSE,
                    opacity = 0.25,
                    hoverinfo = "text",
                    text = ~paste('</br> Date: ', date_vec_bothc, #leaving in case we want to change
                                  '</br> Max Log Copies: ', round(both_ymaxc, digits = 2),
                                  '</br> Min Log Copies: ', round(both_yminc, digits = 2)),
                    name = "",
                    fillcolor = '#E7298A',
                    line = list(color = '#E7298A')) %>%
                layout(yaxis = list(title = "Total Log10 SARS CoV-2 Copies", 
                                 showline = TRUE,
                                 automargin = TRUE)) %>%
                layout(xaxis = list(title = "Date")) %>%
                layout(title = "WRF C") %>%
  plotly::add_markers(x = ~date, y = ~log_total_copies_both,
                      data = wrfc_both,
                       hoverinfo = "text",
                       showlegend = FALSE,
                       text = ~paste('</br> Date: ', date, 
                                     '</br> Actual Log Copies: ', round(log_total_copies_both, digits = 2)),
                       marker = list(color = '#E7298A', size = 6, opacity = 0.65))

p_wrf_c
save(p_wrf_c, file = "./site_objects/wrf_c_year2.rda")

keeping in case

#save(wrfa_both, file = "./plotly_objs/wrfa_both.rda")
#save(wrfb_both, file = "./plotly_objs/wrfb_both.rda")
#save(wrfc_both, file = "./plotly_objs/wrfc_both.rda")
#save(date_vec_botha, file = "./plotly_objs/date_vec_botha.rda")
#save(date_vec_bothb, file = "./plotly_objs/date_vec_bothb.rda")
#save(date_vec_bothc, file = "./plotly_objs/date_vec_bothc.rda")
#save(both_ymina, file = "./plotly_objs/both_ymina.rda")
#save(both_ymaxa, file = "./plotly_objs/both_ymaxa.rda")

#save(both_yminb, file = "./plotly_objs/both_yminb.rda")
#save(both_ymaxb, file = "./plotly_objs/both_ymaxb.rda")

#save(both_yminc, file = "./plotly_objs/both_yminc.rda")
#save(both_ymaxc, file = "./plotly_objs/both_ymaxc.rda")